package com.itheima.d3_udp2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * 目标：完成UDP通信快速入门：实现客户端反复的发。
 */
public class Client {
    public static void main(String[] args) throws Exception{
        //创建客户端对象
        DatagramSocket datagramSocket = new DatagramSocket();

        //封装数据包，发送出去
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要发送的数据：");
            String msg = sc.nextLine();
            byte[] data = msg.getBytes();

            //如果输入的是exit
            if (msg.equals("exit")) {
                break;
            }

            //封装好数据包
            DatagramPacket packet = new DatagramPacket(
                    data,
                    data.length,
                    InetAddress.getByName("127.0.0.1"),
                    6666
            );
            datagramSocket.send(packet);
        }

        //释放资源
        datagramSocket.close();
    }
}
